//=============================================================================
// SettingResourceTask.as
// 
// SettingResourceTask
// 
// AUTHOR:          Jay Liang
// 
//                  Copyright (c) 2011 Break Media
//                              All Rights Reserved.
//                          
//=============================================================================
package core.resources
{
import core.Configuration;

import flash.events.Event;
import flash.net.URLLoader;
import flash.utils.setTimeout;

/**
 * SettingResourceTask
 * @author jliang
 * 
 */
public class SettingResourceTask extends LoadTaskBase
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function SettingResourceTask()
	{
		super();
	}
	
	private var count:int = 0;
	
	private var configList:Array = [
		{ url:"app_config.xml", callBack:appConfigLoaded },
		{ url:"actions.xml", callBack:actionsLoaded },
		{ url:"factory_product.xml", callBack:factoryProductLoaded },
		{ url:"item_resource.xml", callBack:itemResourcesLoaded },
		{ url:"item_building.xml", callBack:itemBuildingLoaded },
		{ url:"item_equipment.xml", callBack:itemEquipmentLoaded },
        { url:"item_pack.xml", callBack:itemPackLoaded },
        { url:"item_skillbook.xml", callBack:itemSkillBookLoaded },
		{ url:"avatar_types.xml", callBack:avatarTypesLoaded },
		{ url:"avatars.xml", callBack:avatarsLoaded },
		{ url:"skills.xml", callBack:skillsLoaded },
		{ url:"tech.xml", callBack:techLoaded },
		{ url:"game_setting.xml", callBack:gameSettingLoaded },
		{ url:"quests.xml", callBack:questSettingLoaded },
		{ url:"game_level.xml", callBack:levelSettingLoaded },
		{ url:"ui_assets.xml", callBack:uiAssetsLoaded },
		{ url:"effects.xml", callBack:effectsLoaded },
		{ url:"ranges.xml", callBack:rangesLoaded },
        { url:"shop_config.xml", callBack:shopConfigLoaded },
		{ url:"npc_avatars.xml", callBack:npcsLoaded }
	];
	
	override public function start():void
	{
		for each (var obj:Object in configList)
		{
			startToLoad(Configuration.configPath + Configuration.ENV + obj.url, obj.callBack);
		}
	}
	
	private function clearLoaderAndGetData(event:Event, func:Function):XML
	{
		var loader:URLLoader = URLLoader(event.target);
		loader.removeEventListener(Event.COMPLETE, func);
		
		count++;
		
		if (count == configList.length)
		{
			setTimeout(complete, 200);
		}
		
		var xml:XML = XML(loader.data);
		return xml;
	}
	
	private function appConfigLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, appConfigLoaded);	
		Configuration.initialize(xml);
	}
	
	private function actionsLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, actionsLoaded);	
		Configuration.configs["actions"] = xml;
	}
	
	private function factoryProductLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, factoryProductLoaded);	
		Configuration.configs["factory_product"] = xml;
	}
	
	private function itemResourcesLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, itemResourcesLoaded);	
		Configuration.configs["item_resource"] = xml;
	}
	
	private function itemBuildingLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, itemBuildingLoaded);	
		Configuration.configs["item_building"] = xml;
	}
	
	private function itemEquipmentLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, itemEquipmentLoaded);	
		Configuration.configs["item_equipment"] = xml;
	}
    
    private function itemPackLoaded(event:Event):void
    {
        var xml:XML = clearLoaderAndGetData(event, itemPackLoaded);	
        Configuration.configs["item_pack"] = xml;
    }
    
    private function itemSkillBookLoaded(event:Event):void
    {
        var xml:XML = clearLoaderAndGetData(event, itemSkillBookLoaded);	
        Configuration.configs["item_skillbook"] = xml;
    }
	
	private function avatarTypesLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, avatarTypesLoaded);	
		Configuration.configs["avatar_types"] = xml;
	}
	
	private function avatarsLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, avatarsLoaded);	
		Configuration.configs["avatars"] = xml;
	}
	
	private function skillsLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, skillsLoaded);	
		Configuration.configs["skills"] = xml;
	}
	
	private function techLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, techLoaded);	
		Configuration.configs["tech"] = xml;
	}
	
	private function gameSettingLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, gameSettingLoaded);
		Configuration.configs["game_setting"] = xml;
	}
	
	private function questSettingLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, questSettingLoaded);
		Configuration.configs["quest"] = xml;
	}
	
	private function levelSettingLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, levelSettingLoaded);
		Configuration.configs["game_level"] = xml;
	}
	
	private function uiAssetsLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, uiAssetsLoaded);	
		Configuration.configs["ui_assets"] = xml;
	}
	
	private function effectsLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, effectsLoaded);	
		Configuration.configs["effects"] = xml;
	}
    
    private function shopConfigLoaded(event:Event):void
    {
        var xml:XML = clearLoaderAndGetData(event, shopConfigLoaded);	
        Configuration.configs["shop"] = xml;
        
        this.complete();
    }
	
	private function rangesLoaded(event:Event):void
	{
		var xml:XML = clearLoaderAndGetData(event, rangesLoaded);	
		Configuration.configs["ranges"] = xml;
	}
	
	private function npcsLoaded(evt:Event):void
	{
		var xml:XML = clearLoaderAndGetData(evt, npcsLoaded);	
		Configuration.configs["npcs"] = xml;
	}
}
}


